package yunwo.cn.yzstation.other;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;



import java.io.File;

import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.utils.DrawableUtils;


public class ImageCut extends ImageView {

	public static final int mRadius =500;
	private boolean isToCutImage = false;
	private Paint mPaint;

	public ImageCut(Context context, AttributeSet attrs) {
		super(context, attrs);
		mPaint = new Paint();
		TypedArray attribute = context.obtainStyledAttributes(attrs, R.styleable.ClipView);
        cutWidth =(int) attribute.getDimension(R.styleable.ClipView_clipWidth, 256f);
        clipColor = attribute.getColor(R.styleable.ClipView_clipColor, 0xaa000000);
        attribute.recycle();
	}
	@SuppressLint("ClickableViewAccessibility")
	@Override
	public boolean onTouchEvent(MotionEvent event) {

		return super.onTouchEvent(event);
	}
	private int cutWidth;
	private int clipColor;
	@SuppressLint("DrawAllocation")
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);

		if (isToCutImage)
			return;
		int width = this.getWidth();
		int height = this.getHeight();
		
		int marginSize = ( getWidth() - cutWidth ) / 2;
				
		mPaint.setColor(clipColor);
		
		// 绘制透明暗色区域
		// top
		canvas.drawRect(0, 0, width, (height - cutWidth) / 2, mPaint);
		// bottom
		canvas.drawRect(0, (height + cutWidth) / 2, width, height, mPaint);
		// left
		canvas.drawRect(0, (height - cutWidth) / 2, marginSize,
				(height + cutWidth) / 2, mPaint);
		// right
		canvas.drawRect(cutWidth + marginSize, (height - cutWidth) / 2, width,
				(height + cutWidth) / 2, mPaint);
		
		//边框线
		mPaint.setColor(Color.WHITE);
		mPaint.setStrokeWidth(2.0f);
		// top
		canvas.drawLine(marginSize, (height - cutWidth) / 2, width - marginSize, (height - cutWidth) / 2, mPaint);
		// bottom
		canvas.drawLine(marginSize, (height + cutWidth) / 2, width - marginSize, (height + cutWidth) / 2, mPaint);
		// left
		canvas.drawLine(marginSize, (height - cutWidth) / 2, marginSize, (height + cutWidth) / 2, mPaint);
		// right
		canvas.drawLine(width - marginSize, (height - cutWidth) / 2, width - marginSize, (height + cutWidth) / 2, mPaint);
	}

	public File onClip() {
		// 获取imageview的bitmap
		// 为了不带半透明的背景，从新刷新下imageview 好获干净的位图 然后截取
		isToCutImage = true;
		invalidate();
		setDrawingCacheEnabled(true);
		Bitmap bitmap = getDrawingCache().copy(getDrawingCache().getConfig(),
				false);
		setDrawingCacheEnabled(false);
		
		// 创建你要截取的位图 这个代码大家估计都非常熟悉
		Bitmap bitmap2 = Bitmap.createBitmap(2 * mRadius, 2 * mRadius,
				Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap2);
		draw(canvas);
		Bitmap cutBitmap = Bitmap.createBitmap(bitmap, (getWidth() - cutWidth) / 2, (getHeight() - cutWidth) / 2, cutWidth, cutWidth);
		File saveFile = null;
		try {
			saveFile = DrawableUtils.saveFile(cutBitmap,
					String.valueOf(System.currentTimeMillis()), 100);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return saveFile;
	}

}
